package cn.bjc.pet.controller;

import cn.bjc.basic.exception.BusinessException;
import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.LoginContext;
import cn.bjc.basic.util.PageList;
import cn.bjc.pet.domain.SearchMasterMsg;
import cn.bjc.pet.dto.AcceptDto;
import cn.bjc.pet.query.SearchMasterMsgQuery;
import cn.bjc.pet.service.ISearchMasterMsgService;
import cn.bjc.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@Controller
//@ResponseBody
@RestController //controller层,返回json数据
@RequestMapping("/searchMasterMsg")
//接口文档的注解
@Api(value = "寻主的API",description="寻主相关的CRUD功能")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    /**
     *  查询所有:
     *  动词:get
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//上面的结合
    @ApiOperation(value = "查询所有寻主信息")
    public List<SearchMasterMsg> findAll(HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);

        return searchMasterMsgService.loadAll();
    }
    
    /**
     * 查询一个:接收参数id
     * url:http://localhost/SearchMasterMsg/id/
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    public SearchMasterMsg findOne(@PathVariable("id") long id) {
        return searchMasterMsgService.loadById(id);
    }

    /**
     * 删除操作
     *  url:http://localhost/searchMasterMsg/id/
     * @param id
     * @return
     * JsonResult:链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 添加或修改操作
     * 
     * @param SearchMasterMsg
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加操作接口")
    public JsonResult updateOrAdd(@RequestBody SearchMasterMsg SearchMasterMsg){
        try {
            if(SearchMasterMsg.getId()==null){
                //添加操作
                searchMasterMsgService.save(SearchMasterMsg);
            } else {
                //修改
                searchMasterMsgService.update(SearchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * 
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query){
        try {
            return searchMasterMsgService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }


    /**
     * 批量删除操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 寻主
     *
     */
    @PostMapping("/publish")
    @ApiOperation(value = "寻主")
    public JsonResult publish(@RequestBody SearchMasterMsg smm,HttpServletRequest request){
        try {
            // 获取当前登录人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);

            searchMasterMsgService.publish(smm,logininfo.getId());
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网路异常!");
        }
    }

    /**
     * 查询待审核
     * @param query
     * @param request
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @PostMapping("/toAudit")//上面的结合
    @ApiOperation(value = "查询待审核")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.toAudit(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     *  待处理
     * @return
     */
    @ApiOperation(value = "待处理查询接口" )
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.toHandle(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     *  用户处理
     * @return
     */
    @ApiOperation(value = "待处理查询接口" )
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {

            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.user(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 拒单操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "拒单")
    public JsonResult reject(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 接单操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PostMapping("/accept")
    @ApiOperation(value = "接单")
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     *  已处理
     * @return
     */
    @ApiOperation(value = "已处理查询接口" )
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.finish(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }
    
}
